package com.itqf.xiaomi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itqf.xiaomi.dto.ProductDto;
import com.itqf.xiaomi.mapper.CategoryMapper;
import com.itqf.xiaomi.mapper.ProductMapper;
import com.itqf.xiaomi.mapper.ProductPictureMapper;
import com.itqf.xiaomi.pojo.Category;
import com.itqf.xiaomi.pojo.Product;
import com.itqf.xiaomi.pojo.ProductPicture;
import com.itqf.xiaomi.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: springboot_part
 *
 * @author: 赵伟风
 * time: 2021/9/26 17:09
 * description:
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductPictureMapper productPictureMapper;

    /**
     * 按分类查询商品
     *
     * @param productDto
     * @return
     */
    @Override
    public Object getPromoProduct(String productDto) {

        //1.查询类别对应的id

        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name", productDto);

        List<Category> categories = categoryMapper.selectList(queryWrapper);

        List<Integer> ids = new ArrayList<>();

        //收集到查询类别的id值!
        for (Category category : categories) {
            ids.add(category.getCategoryId());
        }

        //2.根据类别的id查询商品

        QueryWrapper<Product> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("category_id", ids);

        Page<Product> page = new Page<>(1,6);

        Page<Product> page1 = productMapper.selectPage(page, queryWrapper1);

        //3.封装数据
        Map data = new HashMap();
        data.put("code","001");
        data.put("Product", page1.getRecords());

        return data;
    }

    /**
     * 按分类查询热门商品
     *
     * @param productDto
     * @return
     */
    @Override
    public Object getHotProduct(ProductDto productDto) {

        //1.查询类别对应的id

        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("category_name", productDto.getCategoryName());

        List<Category> categories = categoryMapper.selectList(queryWrapper);

        List<Integer> ids = new ArrayList<>();

        //收集到查询类别的id值!
        for (Category category : categories) {
            ids.add(category.getCategoryId());
        }

        //2.根据类别的id查询商品

        QueryWrapper<Product> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("category_id", ids);
        queryWrapper1.orderByDesc("product_sales");

        Page<Product> page = new Page<>(1,6);

        Page<Product> page1 = productMapper.selectPage(page, queryWrapper1);

        //3.封装数据
        Map data = new HashMap();
        data.put("code","001");
        data.put("Product", page1.getRecords());

        return data;
    }

    /**
     * 案列别查询商品的接口业务
     *
     * @param productDto
     * @return
     */
    @Override
    public Object getProductByCategory(ProductDto productDto) {

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        /**
         * 根据类别id查询商品!
         * 先行判断了下类别id数组是否有值!
         */
        if (productDto.getCategoryID() != null && productDto.getCategoryID().length > 0){
            queryWrapper.in("category_id",productDto.getCategoryID());
        }

        //装配分页对应的参数
        Page<Product> page = new Page<>(productDto.getCurrentPage(),
                productDto.getPageSize());

        //数据查询
        page = productMapper.selectPage(page,queryWrapper);

        //数据封装
        Map data = new HashMap();
        data.put("code","001");
        data.put("Product", page.getRecords());
        data.put("total", page.getTotal());

        return data;
    }

    /**
     * 查询商品类别数据
     *
     * @return
     */
    @Override
    public Object getCategory() {

        List<Category> categories = categoryMapper.selectList(null);

        Map data  = new HashMap();
        data.put("code","001");
        data.put("category", categories);

        return data;
    }

    /**
     * 查询商品详情
     *
     * @param productID
     * @return
     */
    @Override
    public Object getDetails(Integer productID) {

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("product_id",productID);

        List<Product> products = productMapper.selectList(queryWrapper);

        Map data = new HashMap();
        data.put("code","001");
        data.put("Product", products);

        return data;
    }

    /**
     * 查询商品详情以及对应图片
     *
     * @param productID
     * @return
     */
    @Override
    public Object getDetailsPicture(Integer productID) {

        QueryWrapper<ProductPicture> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("product_id",productID);

        List<ProductPicture> productPictures = productPictureMapper.selectList(queryWrapper);

        Map data = new HashMap();
        data.put("code","001");
        data.put("ProductPicture", productPictures);

        return data;
    }
}
